//
//  PublicUtils.m
//  IceBreaker
//
//  Created by Chiao Wolfire on 12-10-10.
//  Copyright (c) 2012年 NDS. All rights reserved.
//

#import "PublicUtils.h"

@implementation PublicUtils

+(void) alertMsgWithTitle: (NSString *)title 
                  message: (NSString *)msg 
        cancelButtonTitle: (NSString *)buttonTitle {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title 
                                                    message: msg 
                                                   delegate: nil 
                                          cancelButtonTitle: buttonTitle 
                                          otherButtonTitles: nil];
    [alert show];
    [alert release];
}

+(UIColor *)getColor:(NSString *)hexColor {
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt: &red];
    
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt: &green];
    
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt: &blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}

+(UIImage *) cutImageFromImage:(UIImage *)superImage {
    float START_X = 0, START_Y = 0, WIDTH = 306, HEIGHT = 190;
    CGSize subImageSize = CGSizeMake(WIDTH, HEIGHT);
    float superWidth = superImage.size.width;
    float superHeight = superImage.size.height;
    if (superWidth > WIDTH) {
        START_X = (superWidth - WIDTH) / 2;
    }
    if (superHeight > HEIGHT) {
        START_Y = (superHeight - HEIGHT) / 2;
    }
    // the rect to cut
    CGRect subImageRect = CGRectMake(START_X, START_Y, WIDTH, HEIGHT);
    CGImageRef imageRef = superImage.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, subImageRect);
    UIGraphicsBeginImageContext(subImageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, subImageRect, subImageRef);
    UIImage* subImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    // the image cut
    return subImage;
}

+(UIImage *) scaleFromImage: (UIImage *)image toSize: (CGSize)size {
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

@end
